#include "effectivenesstester.h"

#include <QCoreApplication>

#include "plwordnetdao.h"

#include <QDebug>

EffectivenessTester::EffectivenessTester(const QDir &queriesDir,QStringList &stoplist, int resultNumber,QObject *parent) :
    QObject(parent)
{
    foreach(const QString &file, queriesDir.entryList())
    {
        queries << Query(queriesDir.absolutePath() + "/" + file, stoplist, resultNumber);
    }
    client = new RESTClient();
    connect(client, SIGNAL(finished()), this, SLOT(finishedSlot()));
}

void EffectivenessTester::finishedSlot()
{
    QList<SearchResult> foundDocs(client->getFoundDocs());
    double avgRecall = 0, avgPrecision = 0, avgFMeasure = 0, avgReciprocalRank = 0;
    double avgTime = 0;
    for(int i=0; i < queries.size(); i++){
        const QStringList &aDocs = foundDocs[i].docs;
        const QStringList &qDocs = queries[i].getDocs();
        int tp = 0;     // true positive
        int j = 0;
        reciprocalRank << 0.;
        foreach(const QString &aDoc, aDocs){
            j++;
            if(qDocs.contains(QString("ccl-%1.xml").arg(aDoc))){
                tp++;
                reciprocalRank[i] = 1. / j;
            }
        }

        recall << double(tp) / qDocs.size();
        if(aDocs.size() > 0)
            precision << double(tp) / aDocs.size();
        else
            precision << 0.;
        avgRecall += recall.last();
        avgPrecision += precision.last();
        avgFMeasure += recall.last() + precision.last() > 0 ?
                    2 * recall.last() * precision.last() / (recall.last() + precision.last()) : 0;
        avgReciprocalRank += reciprocalRank.last();
        avgTime += foundDocs[i].time;
        qDebug() << QString("recall:%1, precision:%2, RR:%3, %4").arg(recall[i], 6, 'f', 3)
                    .arg(precision[i], 6, 'f', 3).arg(reciprocalRank[i], 6, 'f', 3).arg(queries[i].getQuery());
    }
    avgRecall /= queries.size();
    avgPrecision /= queries.size();
    avgFMeasure /= queries.size();
    avgReciprocalRank /= queries.size();
    avgTime /= queries.size();

    avgFMeasure = 2 * avgRecall * avgPrecision / (avgRecall + avgPrecision);

    qDebug() << QString("----------------");
    qDebug() << QString("recall:%1, precision:%2, F-measure:%3, RR:%4, time:%5 <- %6").arg(avgRecall, 6, 'f', 3)
                .arg(avgPrecision, 6, 'f', 3).arg(avgFMeasure, 6, 'f', 3).arg(avgReciprocalRank, 6, 'f', 3)
                .arg(avgTime, 6, 'f', 3).arg("averages");

    qApp->quit();
}
